package lzm.upload;

import lzm.utils.Doc2Pdf;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileUploaderDownloader {
    private static File uploadedFile;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Word2Pdf");
        int frameWidth = 1000;
        int frameHeight = 800;
        frame.setSize(frameWidth, frameHeight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);

        // 按钮尺寸
        int buttonWidth = 200;
        int buttonHeight = 50;

        frame.setLocationRelativeTo(null);

        // 上传按钮居中位置
        JButton uploadButton = new JButton("上传Word");
        uploadButton.setBounds((frameWidth - buttonWidth) / 2 - 100, (frameHeight - buttonHeight) / 2, buttonWidth, buttonHeight);

        // 下载按钮居中位置
        JButton downloadButton = new JButton("下载Pdf");
        downloadButton.setBounds((frameWidth - buttonWidth) / 2 + 100, (frameHeight - buttonHeight) / 2, buttonWidth, buttonHeight);
        downloadButton.setEnabled(false); // 初始状态禁用下载按钮

        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showOpenDialog(frame);

                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try {
                        uploadedFile = transWord2Pdf(selectedFile);
                        JOptionPane.showMessageDialog(frame, "文件上传成功！");
                        downloadButton.setEnabled(true); // 启用下载按钮
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(frame, "文件上传失败：" + ex.getMessage());
                    }
                }
            }
        });

        downloadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (uploadedFile != null && uploadedFile.exists()) {
                    JFileChooser saveFileChooser = new JFileChooser();
                    saveFileChooser.setDialogTitle("选择文件保存位置");
                    saveFileChooser.setSelectedFile(new File(uploadedFile.getName())); // 默认文件名

                    int userSelection = saveFileChooser.showSaveDialog(frame);
                    if (userSelection == JFileChooser.APPROVE_OPTION) {
                        File saveFile = saveFileChooser.getSelectedFile();
                        try {
                            Files.copy(uploadedFile.toPath(), saveFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                            JOptionPane.showMessageDialog(frame, "文件下载成功！");
                        } catch (IOException ex) {
                            JOptionPane.showMessageDialog(frame, "文件下载失败：" + ex.getMessage());
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(frame, "没有可下载的文件！");
                }
            }
        });

        frame.add(uploadButton);
        frame.add(downloadButton);
        frame.setVisible(true);
    }

    private static File transWord2Pdf(File wordFile) throws Exception {
        System.out.println("开始转换文件：" + wordFile.getName());
        File file = Doc2Pdf.doc2pdf(wordFile, "D:\\temp\\" + wordFile.getName().substring(0, wordFile.getName().lastIndexOf(".")) + ".pdf");
        System.out.println("文件转换成功");
        return file;
    }

    private static File uploadFile(File file) throws IOException {
        Path targetDirectory = Paths.get("D:\\temp");
        if (!Files.exists(targetDirectory)) {
            Files.createDirectories(targetDirectory); // 创建目录
        }
        Path targetPath = targetDirectory.resolve(file.getName());
        Files.copy(file.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); // 复制文件到目标目录
        return targetPath.toFile();
    }
}
